package cn.ylg.com.ontouchdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //最大的时间间隔
    private  int maxTime = 2000;
    private long lastTime = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /*如果返回true 则代表onKeyDown()的点击事件被拦截了,如果是false就代表等待消化该事件,该事件没有被处理
    *快速点击两次返回;
    * Touch事件是从父容器-->传递给子容器的
    * */

    /**
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断点击的是否是返回键;
        if (keyCode==KeyEvent.KEYCODE_BACK){
            //获取当前时间;
            long currentTime =  System.currentTimeMillis();
            if (currentTime-lastTime>maxTime){
                Toast.makeText(MainActivity.this, "再来一次就可以退出了", Toast.LENGTH_SHORT).show();
                lastTime = currentTime;
                return true;
            }else{
                finish();
            }
        }
        return super.onKeyDown(keyCode,event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);

    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        return super.onKeyShortcut(keyCode, event);
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        return super.onKeyMultiple(keyCode, repeatCount, event);
    }

}
